<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="app">
        <header>
            <h2>todolist</h2>
            <input type="text" placeholder="请输入" @keyup="add" v-model="content">
        </header>
        <div>
            <h2>正在进行</h2>
            <ul>
                <li v-for="(item, i) in tasks" :key="item.id" v-show="!item.check">
                    <input type="checkbox" v-model="item.check" @click="change(item)">
                    <span>{{item.task}}</span>
                    <button @click="del(i)"> 删除</button>
                </li>
            </ul>
        </div>
        <div>
            <h2>已经完成</h2>
            <ul>
                <li v-for="item in tasks" :key="item.id" v-show="item.check">
                    <input type="checkbox" v-model="item.check"  @click="change(item)">
                    <span>{{item.task}}</span>
                </li>
            </ul>
        </div>
    </div>
    <script src="./vue.js"></script>
    <script>
        new Vue({
            el: '#app',
            data: {
                tasks: [],
                content: ''
            },
            created() {
                this.tasks = JSON.parse(localStorage.getItem('tasks')) || []
            },
            methods: {
                add(e) {
                    if(e.keyCode === 13) {
                        let obj = {
                            id: new Date().getTime(),
                            task: this.content,
                            check: false
                        }
                        this.tasks.push(obj)
                        this.saveTask()
                    }
                     
                },

                change(item) {
                    item.check = !item.check
                    this.saveTask()
                },

                saveTask() {
                    localStorage.setItem('tasks', JSON.stringify(this.tasks))
                },

                del(i) {
                    this.tasks.splice(i, 1)
                    this.saveTask()
                }
            }
        })
    </script>
</body>
</html>